package com.ahdy.service.impl;

import com.ahdy.entity.MedicationRecord;
import com.ahdy.entity.MedicationStatisticsDTO;
import com.ahdy.mapper.MedicationRecordMapper;
import com.ahdy.service.MedicationRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * 用药记录服务实现类
 */
@Service
public class MedicationRecordServiceImpl implements MedicationRecordService {

    @Autowired
    private MedicationRecordMapper medicationRecordMapper;

    @Override
    @Transactional
    public Integer addMedicationRecord(MedicationRecord record) {
        medicationRecordMapper.insert(record);
        return record.getId();
    }

    @Override
    @Transactional
    public int updateMedicationRecord(MedicationRecord record) {
        return medicationRecordMapper.update(record);
    }

    @Override
    @Transactional
    public int deleteMedicationRecord(Integer id) {
        return medicationRecordMapper.deleteById(id);
    }

    @Override
    public MedicationRecord getMedicationRecordById(Integer id) {
        return medicationRecordMapper.findById(id);
    }

    @Override
    public MedicationRecord getMedicationRecordByTaskId(Integer taskId) {
        return medicationRecordMapper.findByTaskId(taskId);
    }

    @Override
    public List<MedicationRecord> getMedicationRecordsByUserId(Integer userId) {
        return medicationRecordMapper.findByUserId(userId);
    }

    @Override
    public List<MedicationRecord> getMedicationRecordsByUserIdAndDateRange(Integer userId, LocalDate startDate, LocalDate endDate) {
        return medicationRecordMapper.findByUserIdAndDateRange(userId, startDate, endDate);
    }

    @Override
    @Transactional
    public int deleteMedicationRecordsByUserId(Integer userId) {
        return medicationRecordMapper.deleteByUserId(userId);
    }

    @Override
    @Transactional
    public int deleteMedicationRecordsByTaskId(Integer taskId) {
        return medicationRecordMapper.deleteByTaskId(taskId);
    }

    @Override
    public List<MedicationStatisticsDTO> getMedicationComplianceStatistics(Integer userId, LocalDate startDate, LocalDate endDate) {
        return medicationRecordMapper.getMedicationComplianceStatistics(userId, startDate, endDate);
    }

    @Override
    public List<Map<String, Object>> getDelayStatistics(Integer userId, Integer days) {
        return medicationRecordMapper.getDelayStatistics(userId, days);
    }

    @Override
    public List<Map<String, Object>> getMedicationRecordDetailsWithMedication(Integer userId, Integer limit) {
        return medicationRecordMapper.findRecordDetailsWithMedication(userId, limit);
    }
} 